﻿#include "VAOneXMLAdaptorAcousticsNCTMultipleNoiseControl.h"

#include "FITKAcousticsPhysics/FITKAcousticsNCTMultipleNoise.h"
#include "rapidxml.hpp"
#include "VAOneXMLAdaptorAcousticsNCTMultipleNoiseWeight.h"

#include <QDebug>

namespace IO
{

    bool VAOneXMLAdaptorAcousticsNCTMultipleNoiseControl::adaptR()
    {
        return readNCTMultipleNoiseControl();
    }

    bool VAOneXMLAdaptorAcousticsNCTMultipleNoiseControl::adaptW()
    {
        return writeNCTMultipleNoiseControl();
    }

    bool VAOneXMLAdaptorAcousticsNCTMultipleNoiseControl::readNCTMultipleNoiseControl()
    {
        if (!_node)// 判断节点是否存在
        {
            return false;
        }

        bool b = true;
        //转换类型
        Acoustics::FITKAcousticsNCTMultipleNoise* data = dynamic_cast<Acoustics::FITKAcousticsNCTMultipleNoise*>(_dataObj);

        if(!data)
        {
            return false;
        }

        //获取名称节点
        auto nameNode = _node->first_node("Name");

        if (nameNode == nullptr)
        {
            //材料数据中 名称为必要项 不可为空 并抛出异常日志
            consoleMessage(2, "PhysicalPropertyBeam name is null");
            return false;
        }

        QString strName = readStrNodeValue(nameNode, &b);

        if(b)
        {
            data->setDataObjectName(strName);//名称可以为空，但是节点不能为空
        }

        //获取颜色节点
        auto colorNode = _node->first_node("Color");
        float colorR = 0; float colorG = 0; float colorB = 0;
        //根据节点获取颜色
        readColorNodeValue(colorNode, colorR, colorG, colorB);
        //设置颜色
        data->setColor(colorR, colorG, colorB);
        readNCTMultipleNoiseWeight();
        return true;
    }

    bool VAOneXMLAdaptorAcousticsNCTMultipleNoiseControl::writeNCTMultipleNoiseControl()
    {
        if (!_node)// 判断节点是否存在
        {
            return false;
        }

        //转换类型
        Acoustics::FITKAcousticsNCTMultipleNoise* data = dynamic_cast<Acoustics::FITKAcousticsNCTMultipleNoise*>(_dataObj);

        if(!data)
        {
            return false;
        }

        // 创建名称节点
        createNodeElement(_node, "Name", data->getDataObjectName());
        // 创建备注节点
        createNodeElement(_node, "Note", "");

        //创建颜色节点
        auto colorNode = createNodeElement(_node, "Color");

        //写入颜色节点信息
        if(colorNode)
        {
            float rgb[3] = {0.1f, 0.3f, 0.4f};
            data->getColor(rgb);
            writeAttribute(colorNode, "colorR", rgb[0]);
            writeAttribute(colorNode, "colorG", rgb[1]);
            writeAttribute(colorNode, "colorB", rgb[2]);
        }

        //写重信息
        writeNCTMultipleNoiseWeight();
        return true;
    }

    bool VAOneXMLAdaptorAcousticsNCTMultipleNoiseControl::readNCTMultipleNoiseWeight()
    {
        auto treatmentNode = _node->first_node("Treatment");

        if(!treatmentNode)//如果为空，则返回
        {
            return true;
        }

        int index = 0;
        bool b = true;
        IO::VAOneXMLAdaptorAcousticsNCTMultipleNoiseWeight* adaptor{};

        while (treatmentNode)
        {
            Acoustics::FITKAcousticsNCTMultipleNoiseWeight* data = new Acoustics::FITKAcousticsNCTMultipleNoiseWeight;

            if(!adaptor)
            {
                adaptor = FITKIOADAPTORFACTORY->createT<IO::VAOneXMLAdaptorAcousticsNCTMultipleNoiseWeight>("VAOneXML", data);
            }

            if(!adaptor)
            {
                //读写器创建异常
                consoleMessage(3,"create adaptor111 Error");
                return false;
            }

            adaptor->setNode(treatmentNode);
            adaptor->setDataObject(data);
            b &= adaptor->adaptR();
            //转换成多层声学包类
            Acoustics::FITKAcousticsNCTMultipleNoise* dataLayUp = dynamic_cast<Acoustics::FITKAcousticsNCTMultipleNoise*>(_dataObj);

            //在这里判断是否存在读取的层信息
            auto layerDataOld = dataLayUp->getDataByIndex(index);

            if(!layerDataOld)//如果为空，则插入到数据类中
            {
                dataLayUp->insertDataObj(index, data);
            }
            else//不为空，则复制当前数据类
            {
                layerDataOld->copy(data);
                delete data;//删除创建的数据
            }

            treatmentNode = treatmentNode->next_sibling("Treatment");
            index++;
        }

        delete adaptor;//删除操作器
        adaptor = nullptr;
        //读取成功
        return b;
    }

    bool VAOneXMLAdaptorAcousticsNCTMultipleNoiseControl::writeNCTMultipleNoiseWeight()
    {
        if (!_node)// 判断节点是否存在
        {
            return false;
        }

        //转换类型
        Acoustics::FITKAcousticsNCTMultipleNoise* data = dynamic_cast<Acoustics::FITKAcousticsNCTMultipleNoise*>(_dataObj);

        if(!data)
        {
            return false;
        }

        bool b = true;
        //获取重数量
        int dataCount = data->getDataCount();
        // 使用适配器写入数据
        IO::VAOneXMLAdaptorAcousticsNCTMultipleNoiseWeight* adaptor{};

        //循环遍历写重xml节点信息
        for(int i = 0; i < dataCount; i++)
        {
            // 创建重节点
            rapidxml::xml_node<char>* node = createNodeElement(_node, "Treatment");
            auto weightData = data->getDataByIndex(i);

            if(!weightData)
            {
                continue;
            }

            if(!adaptor)
            {
                adaptor = FITKIOADAPTORFACTORY->createT<IO::VAOneXMLAdaptorAcousticsNCTMultipleNoiseWeight>("VAOneXML", weightData);
            }

            if (!adaptor)
            {
                // 适配器创建失败，输出错误信息并返回失败
                consoleMessage(3, "create adaptor Error");
                return false;
            }

            adaptor->setNode(node);
            adaptor->setDataObject(weightData);

            // 执行数据写入
            b &= adaptor->adaptW();

        }

        delete adaptor;
        adaptor = nullptr;
        return b;
    }

}
